#!/usr/bin/env perl
use strict;
use warnings;
use diagnostics;
use Test::Simple tests => 23;

use lib "$ENV{SKAL_ROOT}/lib";
use UtilLib qw(get_random_password user_is_valid);
# Testing the UtilLib module

my $db = DBM::Deep->new(
    file    => $UtilLib::DB_FILE{USERS},
    locking => 1
);

foreach (keys %$db) {
    ok(
        user_is_valid($_ . ':' . $db->{$_}) == 1,
        'User and its password are both ok'
    );
    ok(
        user_is_valid($_ . ':' . $db->{$_} . 'badpassword') == 0,
        'User is ok and its password is not ok'
    );
    ok(
        user_is_valid($_ . 'baduser' . ':' . $db->{$_}) == 0,
        'User is not ok and its password is ok'
    );
    ok(
        user_is_valid($_ . 'baduser' . ':' . $db->{$_} . 'badpassword') == 0,
        'User and its password are both not ok'
    );
}


foreach (-2 .. 20) {
    if ($_ < 8 || $_ > 18) {
        ok(! eval { get_random_password($_) }, 'Bad password length');
    }
    else {
        ok($_ + 1 == length get_random_password($_), 'Good password length');
    }
}
# Total of 23 tests

